package com.ljs.spring_fastdfs.controller;

import com.ljs.spring_fastdfs.bean.TUser;
import com.ljs.spring_fastdfs.config.util.JsonResult;
import com.ljs.spring_fastdfs.config.util.UploadAndDownloadUtil;
import com.ljs.spring_fastdfs.exception.DataResult;
import com.ljs.spring_fastdfs.service.TuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@Api("注册")
@Slf4j
public class Add {
    //读取配置文件自定义的路径
    @Value("${myfile.staticAccessPath}")
    String staticAccessPath;
    @Value("${myfile.uploadFolder}")
    String uploadFolder;
    @Resource
    TuserService tuserService;

    @RequestMapping("/toadd")
    @ApiOperation(value = "跳转到注册")
    public String toadd(){
        return "add";
    }
    @RequestMapping("/tologin")
    @ApiOperation(value = "跳转到注册")
    public String tologin(){
        return "login";
    }
    @RequestMapping("/show")
    @ApiOperation(value = "跳转到show")
    public String show(){
        return "show";
    }
    @RequestMapping("/logout")
    @ApiOperation(value = "退出")
    public String logout(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.removeAttribute("tUser");
        return "login";
    }
    @RequestMapping("/login")
    @ApiOperation(value = "登录")
    @ResponseBody
    public JsonResult login(@RequestParam("name") String name, HttpServletRequest request){
        JsonResult jsonResult=new JsonResult();
        System.out.println("name"+name);
        if(name!=null||name.equals("")){
        TUser tUser=    tuserService.loginbyname(name);
        if(tUser!=null){
            HttpSession session=request.getSession();
            session.setAttribute("tUser",tUser);
            jsonResult.setMsg("ok");
        }else{
            jsonResult.setMsg("登录失败");
        }
        }


        return jsonResult;
    }
    @RequestMapping("/add")
    @ApiOperation(value = "注册")
    @ResponseBody
    public DataResult add(@RequestParam("fileName")MultipartFile file , @RequestParam("name")String name) throws Exception {
        DataResult dataResult=new DataResult();
        System.out.println("账号是"+name);
        System.out.println("文件的大小"+file.getSize());
        System.out.println("要上传的路径");

        String finaname="";
        System.out.println("要上传的路径");
        //判断是不是空
        if (file.isEmpty()) {
            dataResult.setMsg("文件为空");
            log.info("是空");

        }  else {
            // String filePath = "E:/images/"; // 上传到本地的路径 ,要加mvc的拦截器才能访问到物理路径的图片
            String filename = file.getOriginalFilename();//文件名
            System.out.println("文件名" + filename);
            String sufix = filename.substring(filename.lastIndexOf('.'));
            System.out.println("后缀是" + sufix);
            if (sufix.equals(".jpg") || sufix.equals(".jpeg") || sufix.equals(".png") || sufix.equals(".gif")) {
                filename = UUID.randomUUID() + sufix;

                File file1 = new File(uploadFolder + filename);//这里用自己配置的路径
                if (!file1.getParentFile().exists()) {//如果父文件不存在
                    log.info("不存在");
                    file1.getParentFile().mkdir();//创建父文件
                }

                try {
                    file.transferTo(file1);//上传
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finaname = "/img/" + filename;//传入前端的名称

                System.out.println("最后的路径" + finaname);
                TUser tUser = new TUser();
                if (name != null || !name.equals("") && finaname != null) {
                    tUser.setName(name);
                    tUser.setPic(finaname);
                    System.out.println("要添加的" + tUser);

                    int i = tuserService.insert(tUser);
                    if (i > 0) {
                        log.info("注册成功");
                        dataResult.setMsg("ok");
                    }
                }


            }else {
                log.error("格式不对");
                dataResult.setMsg("图片格式不对");
            }

        }







        return dataResult;
    }



}
